<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>子传父</title>
</head>
<body>
    <div id="app">
        <!-- 
            子传父:
                1.在子组件中通过$emit()方法定义事件,参数1:'自定义事件名',参数2:要传给父组建的参数
                2.在父组件中通过v-on指令监听自定义事件,然后在监听的回调中取得数据
         -->
        <cpn @itemclick="getChild"></cpn>
    </div>


    <template id="cpn">
        <div>
            <button v-for="item in categroies" :key="item.id" @click="getData(item)">{{item.name}}</button>
        </div>
    </template>

    <script src="../vue/vue.js"></script>
    <script>
        //定义子组件
        const cpn = {
            template:'#cpn',
            data() {
                return {
                    //类别
                    categroies:[
                        {
                            id:'aaa',
                            name:'热门推荐'
                        },
                        {
                            id:'bbb',
                            name:'手机数码'
                        },
                        {
                            id:'ccc',
                            name:'电脑办公'
                        }
                    ]
                }
            },
            methods: {
                getData(item){
                    //子传父:1.通过自定义事件
                    this.$emit('itemclick',item);
                }
            },
        }
        const app = new Vue({
            el:'#app',
            data:{
                
            },
            // 注册子组件
            components:{
                cpn
            },
            methods: {
                //2.在父组件中取得数据
                getChild(item){
                    console.log(item);
                }
            },
        });
    </script>
</body>
</html>